package com.ruoyi.crm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.crm.domain.ContactInfo;
import com.ruoyi.crm.mapper.ContactMapper;
import com.ruoyi.crm.service.IContactService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@RequiredArgsConstructor
@Service
public class ContactServiceImpl implements IContactService {
    private final ContactMapper contactMapper;


    /**
     *
     *查询联系人列表
     */


    @Override
    public TableDataInfo<ContactInfo> selectPageContactInfoList(ContactInfo contactInfo, PageQuery pageQuery) {
        //分页查询方法二：对数据库进行操作
        LambdaQueryWrapper<ContactInfo> lqw = new LambdaQueryWrapper<ContactInfo>()
            .like(StringUtils.isNotBlank(contactInfo.getContactName()), ContactInfo::getContactName, contactInfo.getContactName())
            .or()
            .inSql(ContactInfo::getCustomerId,"select customer_id from crm_customer_info where customer_name like '%"+contactInfo.getCustomerId()+"%'" )
            .or()
            .like(StringUtils.isNotBlank(contactInfo.getPhone()), ContactInfo::getPhone, contactInfo.getPhone());
        Page<ContactInfo> page = contactMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);

    }
    /**
     * 根据联系人ID获取详细信息
     *
     * @param contactId 联系人ID
     */

    @Override
    public ContactInfo selectContactById(Long contactId) {
        return contactMapper.selectById(contactId);
    }

    @Override
    public List<ContactInfo> selectContactList(ContactInfo contactInfo) {
        return contactMapper.selectList(new LambdaQueryWrapper<ContactInfo>()
            .like(StringUtils.isNotBlank(contactInfo.getContactName()), ContactInfo::getContactName, contactInfo.getContactName())
            .inSql(ContactInfo::getCustomerId,"select customer_id from customer_info where customer_name like '%"+contactInfo.getCustomerId()+"%'" )
            .like(StringUtils.isNotBlank(contactInfo.getPhone()), ContactInfo::getPhone, contactInfo.getPhone()));
    }
    /**
     *新增联系人
     */

    @Override
    public int insertContact(ContactInfo contactInfo) {
        return contactMapper.insert(contactInfo);
    }
    /**
     *修改联系人
     */
    @Override
    public int updateContact(ContactInfo contactInfo) {
        return contactMapper.updateById(contactInfo);
    }
    /**
     * 删除联系人
     */
    @Override
    public int delectContact(Long contactId) {
        return contactMapper.deleteById(contactId);
    }


}
